<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

  class User
  { 
  	
    private static $instance;
    
    private $role   = array();
    
    private $session;
    
    const ALLOW = true;
    const DENIED = false;
    
    /**
     * Create Instance
     * @param int $i
     * @return object
     */                   
    public static function getInstance(){
    
      if(!(self::$instance instanceof User)){
         self::$instance = new User();
      }
      
      return self::$instance;
    }

    
    public function __construct(){
      $session =& new Session();
      $this->session = $session->getNamespace("Miffos\User\authorization");
      
    }
    
    
    /**
     * Add role
     * @param string $roleName
     */
    public function addRole($roleName){
    	
    	$this->role[$roleName] = array();
    }
    
    
    
    /**
     * Add rule
     * @param string $roleName
     * @param string|array $nameRule
     * @param bool $rule     
     */
    public function addRule($roleName,$nameRule,$rule){
    	
    	if(is_array($nameRule)){
    		
    		foreach($nameRule as $name){
    		
    			$this->role[$roleName][$name] = $rule;
    		}
    	}else{
    		
    		$this->role[$roleName][$nameRule] = $rule;
    	}
    }
	
    
    
    /**
     * Allow rule
     * @param string $roleName
     * @param string|array $nameRule
     */
    public function allow($roleName,$nameRule){
    	
    	$this->addRule($roleName,$nameRule,self::ALLOW);
    }
    
    
    
    /**
     * Denied rule
     * @param string $roleName
     * @param string|array $nameRule
     */
    public function denied($roleName,$nameRule){
    	
    	$this->addRule($roleName,$nameRule,self::DENIED);
    }
    
    

    /**
     * Autorization
     * @param string $username
     * @param string $password
     * @param array $data     
     */
    public function authorization($username,$password = null,$data=array()){
    	
      $this->session->username = $username;
      $this->session->password = $password;
      $this->session->data     = $data;
      
    }
    
    
    
    /**
     * Is Autorization ?
     * @param string $username
     * @return bool
     */
    public function isAuthorization()
    {
        return isset($this->session->username);
    }
    
    
    
    /**
     * Set Role
     * @param string $nameRole
     */     
    public function setRole($nameRole){
    	
    	$this->session->role = $nameRole;
    	
    }
    
      
    
    /**
     * If Allow
     * @param string $name
     * @return bool     
     */
  	public function isAllow($name){
  		
  		return $this->role[$this->session->role][$name];
  	}
  	
  	
    
    /**
     * If Denied
     * @param string $name
     * @return bool     
     */
  	public function isDenied($name){
  		
  		return !$this->role[$this->session->role][$name];
  	}




  	/**
  	 * Set expire
  	 */
  	public function setExpiration($sec){

        $this->session->setExpiration($sec);

    }


    
  	/**
  	 * Logout
  	 */       	
  	public function logout(){
    
        unset($this->session->username,$this->session->password,$this->session->role,$this->session->data);
    
    }
    

    public function getUsername(){
        return $this->session->username;
    }


    public function getPassword(){
        return $this->session->password;
    }


    public function getRole(){
        return $this->session->role;
    }


    public function getData(){
        return $this->session->data;
    }

  }
  
?>